<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <!--<a-col :md="8" :sm="24">
            <a-form-item label="服务商编号">
              <a-input v-model="queryParam.code" placeholder="请输入"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="用户姓名">
              <a-input v-model="queryParam.username" placeholder="请输入"/>
            </a-form-item>
          </a-col>-->
          <a-col :md="8" :sm="24">
            <a-form-item label="状态">
              <a-select placeholder="请选择" v-model="queryParam.status">
                <a-select-option value="">全部</a-select-option>
                <a-select-option value="0">禁用</a-select-option>
                <a-select-option value="1">启用</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="服务商">
              <a-tree-select
                :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
                placeholder='请选择商户'
                allowClear
                treeDefaultExpandAll
                :treeData="treeData"
                v-model="queryParam.code"
              >
              <span style="color:#08c" slot="title" slot-scope="{key}"
                    v-decorator="['key',{rules: [{ required: true, message: '请选择商户' }]}]">
                {{value}}
              </span>>
              </a-tree-select>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button v-action:query type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button v-action:query style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button v-action:add type="primary" icon="plus" @click="$refs.modal.add()">新建</a-button>
    </div>

    <s-table
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
    >
<!--      是否默认-->
      <a-tag  slot="state" slot-scope="text"  v-if="text==1" color="#2db7f5" >
        {{ text | stateFilter }}
      </a-tag>
      <a-tag  slot="state" slot-scope="text"  v-else color="#f50">
        {{ text | stateFilter }}
      </a-tag >
<!--      状态-->
      <a-tag  slot="status" slot-scope="text"  v-if="text==1" color="#2db7f5">
        {{ text | statusFilter }}
      </a-tag>
      <a-tag  slot="status" slot-scope="text"  v-else color="#f50">
        {{ text | statusFilter }}
      </a-tag >
      <span slot="sourceUrl" slot-scope="text,record">
          <template>
            <div class="dfhead">
              <img v-image-preview :src="record.sourceUrl" alt="" style="width: 50px;height: auto;">
            </div>
        </template>
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a v-action:update @click="$refs.modal.edit(record)">编辑</a>
          <a-divider type="vertical"/>
          <a-dropdown>
          <a class="ant-dropdown-link">
            更多<a-icon type="down"/>
          </a>
          <a-menu slot="overlay">
            <a-menu-item>
              <a v-action:update @click="updateStatus(record)"> {{record.status == 0 ? '启用':'禁用'}}</a>
            </a-menu-item>
            <a-menu-item>
                <a v-action:delete @click="handleDel(record)">删除</a>
            </a-menu-item>
         </a-menu>
        </a-dropdown>


        </template>
      </span>
    </s-table>

    <Edit ref="modal" @ok="handleOk"></Edit>

  </a-card>
</template>

<script>

  import STable from '@/components/table/'
  import Edit from './Edit'
  import { selectList, del,updateStatus} from '@/api/businessService/businessAdvertisement'
  import { selectTree } from '@/api/storeService/store'


  export default {
    components: {
      STable,
      Edit
    },
    //--############  data() ###############&ndash;&gt;
    data() {
      return {
        description: '',
        // 高级搜索 展开/关闭
        advanced: false,
        // 查询参数
        queryParam: {},
        treeData:[],
        // 表头
        columns: [
          {
            title: '广告名称',
            dataIndex: 'name',
            scopedSlots: { customRender: 'name' }
          },
          {
            title: '排序',
            width: '90px',
            dataIndex: 'sort'
          },
          {
            title: '广告图片',
            dataIndex: 'displayTime',
            scopedSlots: { customRender: 'sourceUrl' }
          },

          {
            title: '是否默认',
            dataIndex: 'state',
            width: '150px',
            scopedSlots: { customRender: 'state' }
          },
          {
            title: '状态',
            dataIndex: 'status',
            width: '150px',
            scopedSlots: { customRender: 'status' }
          },
          {
            title: '操作',
            width: '180px',
            dataIndex: 'action',
            scopedSlots: { customRender: 'action' }
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          this.queryParam.type = 0
          return selectList(Object.assign(parameter, this.queryParam))
            .then(res => {
              console.log(res)
              return res.result
            })
        },

        selectedRowKeys: [],
        selectedRows: []
      }
    },
    //--############  data() ###############&ndash;&gt;
    created() {
      this.updateMenu()
      const _this = this
      const params = { 'ctrlType': '', 'displayType': '1' }
      /*ctrlType:控制那些层级结构不能操作，可以不连续，1，服务商，2，代理商，3商户，4，门店
      * displayType:控制那些层级显示，必须是连续，1，服务商，2，代理商，3商户，4，门店
      * */
      selectTree(params).then(res => {
        if (res.code === 200) {
          if (res.result == null) {
            return
          }
          _this.treeData = res.result
        } else {
          this.$notification.error({
            message: '错误',
            description: res.message,
            duration: 4
          })
        }
      })
    },
    //--############  methods() ###############&ndash;&gt;
    methods: {
      updateMenu () {
        const routes = this.$route.matched.concat()
        // this.defaultSelectedKeys = [ routes.pop().path ]
      },
      handleDel(row) {
        const _this = this
        this.$confirm({
          title: '警告',
          content: `真的要删除吗?`,
          okText: '删除',
          okType: 'danger',
          cancelText: '取消',
          onOk() {
            // 在这里调用删除接口
            del(row.id).then(res => {
              if (res.code === 200) {
                _this.$message.success('删除成功')
                _this.$emit('ok')
                _this.$refs.table.refresh()
              }
              else {
                _this.$notification.error({
                  message: '错误',
                  description: res.message,
                  duration: 4
                })
              }
              // _this.$message.info(res.message)

            })
          },
          onCancel() {
            console.log('Cancel')
          }
        })
      }
      ,
      handleOk() {
        // 新增/修改 成功时，重载列表
        this.$refs.table.refresh(false)
      },

      onSelectChange(selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      toggleAdvanced() {
        this.advanced = !this.advanced
      },
      updateStatus: function(row) {
        // 0未上架，1上架
        let state = 1
        if (row.status == 1) {
          state = 0
        } else {
          state = 1
        }
        const body = {
          id: row.id,
          status: state
        }
        updateStatus(body).then(res => {
          if (res.code === 200) {
            this.$message.success('操作成功！')
            //刷新页面
            this.$refs.table.refresh()
          } else {
            this.$notification.error({
              message: '错误',
              description: res.message,
              duration: 4
            })
          }
        })
      },
    },
    //--############  methods() ###############&ndash;&gt;
    //--############  filters() ###############&ndash;&gt;
    filters: {
      stateFilter(state) {
        const statusMap = {
          1: '是',
          0: '否'
        }
        return statusMap[state]
      },
      statusFilter(state) {
        const statusMap = {
          1: '启用',
          0: '禁用'
        }
        return statusMap[state]
      }
    }
  }
  //--############  filters() ###############-->
</script>
